package com.fwj.user.annotation;

import com.alibaba.fastjson.JSONObject;
import com.fwj.core.common.annotation.LoginUser;
import com.fwj.core.common.common.CommonUtils;
import com.fwj.core.common.execption.CommonException;
import com.fwj.core.common.model.ResponseEnum;
import com.fwj.user.api.rbac.RBACService;
import com.fwj.user.entity.user.UserBaseInfo;
import com.fwj.user.model.rbac.role.UserAndRoleVo;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.MethodParameter;
import org.springframework.stereotype.Component;

import java.lang.annotation.*;

/**
 * @Author: fwj
 * @Data: 2022/5/2 14:37
 * @Version 1.0
 * @Description:
 */

@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD,ElementType.FIELD,ElementType.PARAMETER})
public @interface RolePermissions {


    String[] Roles() default {};

    String[] Auth() default  {};

    @Component
    @Aspect
    class RolePermissionsHandle {

        //添加日志工厂
        private static final Logger LOGGER = LoggerFactory.getLogger(RolePermissionsHandle.class);

        @Autowired
        private RBACService rbacService;

        @Pointcut(value = "@annotation(com.fwj.user.annotation.RolePermissions)")
        private void pointCut(){

        }

        @Before("pointCut() && @annotation(rolePermissions)")
        public void before(JoinPoint joinPoint, RolePermissions rolePermissions){
            try {
                Object[] args = joinPoint.getArgs();
                UserBaseInfo userBaseInfo = null;
                for (int i = 0; i < args.length; i++){
                    //匹配当前登录用户信息
                    if (args[i] instanceof UserBaseInfo){
                        userBaseInfo = JSONObject.parseObject(JSONObject.toJSONString(args[i]),UserBaseInfo.class);
                    }
                }
                if (CommonUtils.isEmpty(userBaseInfo)){
                    throw new CommonException(ResponseEnum.USER_NO_LOGIN);
                }
                //获取注解参数
                String[] roles = rolePermissions.Roles();
                String[] auth = rolePermissions.Auth();
                // 校验账户权限是否满足
                if (CommonUtils.isNotEmpty(roles)){ checkUserRole(userBaseInfo, roles); }
                if (CommonUtils.isNotEmpty(auth)){ checkUserAuth(userBaseInfo, auth); }
            } catch (CommonException e) {
                LOGGER.error(e.toString());
                throw new CommonException(ResponseEnum.SERVER_ERROR);
            }
        }

        private void checkUserRole(UserBaseInfo userBaseInfo, String[] roles) {
            for (int i = 0; i < roles.length; i++ ){
                UserAndRoleVo userAndRoleVo = new UserAndRoleVo();
                userAndRoleVo.setUserNumber(userBaseInfo.getNumber());
                userAndRoleVo.setRoleNumber(roles[i]);
                rbacService.queryRelationUserAndRole(userAndRoleVo);
            }
        }

        private void checkUserAuth(UserBaseInfo userBaseInfo, String[] auth) {
            for (int i = 0; i < auth.length; i++ ){
                rbacService.queryRelationUserAndAuth(userBaseInfo.getNumber(), auth[i]);
            }
        }

    }
}
